<?php
/**
 * Created by PhpStorm.
 * User: 热心观众
 * Date: 2018/10/7
 * Time: 12:06
 */
namespace app\admin\controller;

use think\Db;
use think\Session;

class AdminLogin{

    // 后台登录页面
    public function login()
    {
        return view('adminlogin/login');
    }

    // 后台登录处理
    public function aDoLog()
    {
        $username = input('username');
        $password = input('password');

        $data = Db::table('zz_admin')->where('zz_admin_name',$username)->where('zz_admin_password',$password)->find();
        $res = [];
        if (!empty($data) && $data['zz_admin_status'] == 1) {
            Session::set('user_data',$data);
            // 通过 当前登录用户得 id  查询 对应 角色id
            $userRoleId = Db::table('zz_admin_role')->where('zz_admin_id',$data['zz_admin_id'])->select();
            
            // 通过 角色id  查询对应 权限id
            for ($i=0; $i < sizeof($userRoleId); $i++) { 
                $userNodeId[] = Db::table('zz_role_node')->where('zz_role_id',$userRoleId[$i]['zz_role_id'])->select();
            }
            
            // 通过权限id  查询权限信息
            for ($i=0; $i < sizeof($userNodeId); $i++) { 
                for ($j=0; $j < sizeof($userNodeId[$i]); $j++) { 
                    $userNode[] = Db::table('zz_node')->where('zz_node_id',$userNodeId[$i][$j]['zz_node_id'])->find();
                }
            }
            Session::set('userNode',$userNode);
            
            $res['status'] = 1;
            return json($res);
        } else {
            $res['status'] = 0;
            return json($res);
        }
    }
}